import { defineStore } from "pinia";
import router from "@/router";
import { mapMenusToRoutes } from "@/utils/mapMenus.js";
import { routes } from "@/router/routerFile.js";
import menu from "@/pages/config.js";

export const mainStore = defineStore("main", {
    state: () => {
        return {
            tableData: {},
            loading: false,
            userInfo: {},
            menu,
            theme: {},
            btns: [],
            navData: [],
            equipment: {
                equipmentId: "",
                equipmentCode: "",
                equipmentName: ""
            }
        };
    },
    actions: {
        async setMenu(menus) {
            if (!menus) {
                this.menu = JSON.parse(localStorage.getItem("menu"));
            } else {
                this.menu = menus;
                localStorage.setItem("menu", JSON.stringify(this.menu));
            }
            const routeList = mapMenusToRoutes(menu, routes);
            routeList.forEach((route) => {
                router.addRoute("main", route);
            });
        },
        async setTheme(path) {
            const needChangeRoute = ["/main/healthAssessment", "/main/faultDetection", "/main/conditionMonitoring", "/main/conditionStatistic"];
            if (needChangeRoute.includes(path)) {
                this.theme = {
                    bgColor: "#0E2B3D",
                    fontColor: "#fff",
                    subFontColor: "#fff",
                    mainBgColor: "#0C1E28",
                    anotherColor: "#4BA6DD",
                    borderColor: "#4BA6DD",
                    formFontColor: "#C5D8FF"
                };
            } else {
                this.theme = {
                    bgColor: "#fff",
                    fontColor: "#1B3646",
                    subFontColor: "#595959",
                    mainBgColor: "#ecf1f3",
                    anotherColor: "#00428E",
                    borderColor: "#EBEBEB",
                    formFontColor: "#1B3646"
                };
            }
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                paths: ["tableData"]
            }
        ]
    }
});
